import { prop, modelOptions, Ref, arrayProp } from '@typegoose/typegoose'
import { ApiModelProperty } from '@nestjs/swagger'
import { hashSync } from 'bcryptjs'
import { Role } from './role.model'
import { Permission } from './permission.model'

@modelOptions({
  schemaOptions: {
    timestamps: true,
  }
})

export class User {
  @ApiModelProperty({ description: '用户名', example: 'admin' })
  @prop()
  username: string

  @ApiModelProperty({ description: '用户密码', example: '123456aa' })
  @prop({
    select: false,
    get(val) {
      return val;
    },
    set(val) {
      return val ? hashSync(val) : val;
    },
  })
  password: string

  @ApiModelProperty({ description: '拥有角色', example: ['Role._id', 'Role._id'], type: [String] })
  @arrayProp({ itemsRef: Role })
  roles: Ref<Role> []

  @ApiModelProperty({ description: '拥有权限', example: ['Object._id', 'Object._id'], type: [String], required: false })
  @arrayProp({ itemsRef: Permission })
  permission?: Ref<Permission> []
}